package com.sogou.jstormkafkaredis;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

/**
 * @author 01370603
 */
public class MyKafkaProducer {

    public static void main(String[] args) {
        Properties props = new Properties();
//        props.put("bootstrap.servers", "10.144.101.40:9092,10.144.110.44:9092,10.134.83.54:9092");
        props.put("bootstrap.servers", "10.202.34.28:9096,10.202.34.29:9096,10.202.34.30:9096");
        props.put("acks", "all");
        props.put("retries", 0);
        props.put("batch.size", 16384);
        props.put("linger.ms", 1);
        props.put("buffer.memory", 33554432);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        Producer<String, String> producer = new KafkaProducer<>(props);

        FileReader fileReader = null;
        BufferedReader reader = null;
        try {
            String str;
            int i = 0;
            fileReader = new FileReader("src/main/resources/words.txt");
            reader = new BufferedReader(fileReader);
            while ((str = reader.readLine()) != null) {
                producer.send(new ProducerRecord<>("GROUND_DEV_01370603", Integer.toString(i), str));
                System.out.println("partition:" + i % 3 + ",key:" + i + ",value:" + str);
                i++;
//                try {
//                    Thread.sleep(1000 * 5);
//                } catch (InterruptedException e) {
//                }
            }
        } catch (Exception e) {
            throw new RuntimeException("Error reading tuple", e);
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            producer.close();
        }
    }
}
